package com.wry.netty.handler;

import com.wry.netty.utils.CurrencyUtil;
import com.wry.netty.utils.Message;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.nio.charset.Charset;
import java.util.List;

/**
 * <h1> 用于处理输入消息的handler，用于将byte消息转换成Message <h1>
 *
 * @author wry
 * @since 2023/4/18 15:48
 */
public class ServerTransferMsgHandler extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        final String totalMsg = byteBuf.readCharSequence(byteBuf.readableBytes(), Charset.forName("utf-8")).toString();
        final String[] content  = totalMsg.split("~");
        list.add(new Message(content[0],CurrencyUtil.parseDateTime(content[1]), content[2]));
    }
}
